package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.Set;

/**
 * @Author: bowen
 * @Date: 2019/7/2 14:46
 * @Description:
 */
@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {

	@Autowired
	UserDao userDao;

	@Autowired
	RoleDao roleDao;

	@Autowired
	PermissionDao permissionDao;


	@Override
	public User findUserByUsername(String username) {
          User user= userDao.findUserByUserName(username);
          if(ObjectUtils.isEmpty(user)){
          	return null;
          }
          Set<Role> roleSet=roleDao.findRolesByuserId(user.getId());
          if(!CollectionUtils.isEmpty(roleSet)){
	          roleSet.forEach(role->{
		          Set<Permission> permissions=permissionDao.findPermissionsBYRoleId(role.getId());
		          role.setPermissions(permissions);
	          });
	          user.setRoles(roleSet);
          }
		return user;
	}
}
